﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Virgee.Common
{
    internal static class Is
    {
        /// <summary>
        /// Ensures that the given parameter is not null.
        /// </summary>
        /// <typeparam name="T">The type of the parameter</typeparam>
        /// <param name="value">The value of the parameter.</param>
        /// <param name="parameterName">The name of the parameter.</param>
        /// <returns>The value of the parameter if not null, otherwise a <see cref="T:System.ArgumentNullException"/> will be thrown.</returns>        
        [DebuggerStepThrough]
        internal static T NotNull<T>(T value, string parameterName) where T : class
        {
            if (value == null) {
                throw new System.ArgumentNullException(parameterName);
            }
            return value;
        }


        /// <summary>
        /// Ensures that the string parameter is not null or white-space characters only.
        /// </summary>
        /// <param name="value">The value of the parameter.</param>
        /// <param name="parameterName">The name of the parameter.</param>
        [DebuggerStepThrough]
        internal static void NotNullOrWhiteSpace(string value, string parameterName)
        {
            Is.NotNull<string>(value, parameterName);
            Is.True(value.Trim().Length > 0, parameterName, Strings.EmptyOrWhiteSpaceOnlyStringNotAllowed);
        }


        /// <summary>
        /// Ensures that at least one of the objects in the given array is not null.
        /// </summary>
        /// <param name="parameters">The objects array.</param>
        internal static void AtLeastOneNotNull(params object[] parameters)
        {
            if (parameters.All(e => e == null)) {
                throw new System.ArgumentException(Strings.AllParametersAreNull);
            }
        }


        /// <summary>
        /// Ensures that the given expression, which describes the acceptable condition for the specified parameter, evaluates to true.
        /// </summary>
        /// <param name="expression">The expression to be evaluated.</param>
        /// <param name="parameterName">The name of the parameter.</param>
        /// <param name="exceptionMessage">The message to include in the exception that will be thrown in case expression evaluates to false.</param>
        [DebuggerStepThrough]
        internal static void True(bool expression, string parameterName = null, string exceptionMessage = null)
        {
            if (!expression) {
                throw new System.ArgumentException(exceptionMessage ?? Strings.InvalidArgument, parameterName);
            }
        }
    }
}
